﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetPE.Core.DataDirectories;
using NetPE.Dialogs;

namespace NetPE.TreeNodes
{
    public class CertificateDNode : BaseNode
    {
        public CertificateDNode(DataDirectory hdr)
            : base(hdr)
        {
            this.hdr = hdr;
            this.Text = "Certificate Directory";
            this.ImageIndex = this.SelectedImageIndex = NodeType.Directory;
        }

        DataDirectory hdr;
        public override object Data { get { return hdr; } }

        public override BaseEditor GetEditorControl()
        {
            ReadonlyCollectionEditor<CertificateDirectory.CertificateEntry> ret = new ReadonlyCollectionEditor<CertificateDirectory.CertificateEntry>();
            ret.PropertyName = "CertificateType";
            ret.Actions.Add(new PropertyAction() { Name = "Edit Certificate", Callback = new EventHandler(EditCert) });
            return ret;
        }

        void EditCert(object sender, EventArgs e)
        {
            PropertyEditor etr = sender as PropertyEditor;
            CertificateDirectory.CertificateEntry entry = etr.Object as CertificateDirectory.CertificateEntry;
            CertificateDirectory.Certificate cert = entry.LoadCertificate();
            new PropertyDialog(cert).ShowDialog();
            byte[] dat;
            cert.Save(out dat);
            entry.CertificateData = dat;
        }
    }
}
